package com.gitee.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.enums.CookbookTypeEnum;
import com.gitee.enums.ResponseEnum;
import com.gitee.exception.CookbookException;
import com.gitee.mapper.CookbookMapper;
import com.gitee.pojo.Cookbook;
import com.gitee.service.ICookbookService;
import com.gitee.vo.CookbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class CookbookServiceImpl extends ServiceImpl<CookbookMapper, Cookbook> implements ICookbookService {

    @Autowired
    private CookbookMapper cookbookMapper;

    /**
     * 获取今日菜谱
     *
     * @param cookbookType
     * @return
     */
    @Override
    public CookbookVo getTodayCookbook(Integer cookbookType) {
        //查找今天的菜谱，如果大于中午12点，则显示明天的菜谱
        Date eatTime = new Date();
        if (DateUtil.hour(eatTime, true) >= 12) {
            eatTime =  DateUtil.offsetDay(eatTime, 1);
        }
        CookbookVo cookbookVo = cookbookMapper.selectByEatTimeAndCookbookType(DateUtil.format(eatTime, "yyyy-MM-dd"), CookbookTypeEnum.of(cookbookType).getCode());
        if (cookbookVo == null) {
            throw new CookbookException(ResponseEnum.NO_COOKBOOK);
        }
        return cookbookVo;
    }
}
